import { model } from '@/ts/base';
import { deepClone } from '@/ts/base/common';
import { Executor } from '.';
import { FormData } from './index';
import { FieldsChangeExecutor } from '@/ts/base/model';
import { formatDate } from '@/utils';

/**
 * 字段变更
 */
export class FieldsChange extends Executor<FieldsChangeExecutor> {
  /**
   * 执行
   * @param data 表单数据
   */
  async execute(data: FormData): Promise<boolean> {
    const instance = this.task.instanceData;
    if (instance) {
      for (const change of this.metadata.changes) {
        for (const form of [
          ...instance.node.primaryForms,
          ...instance.node.detailForms,
        ]) {
          if (change.id == form.id) {
            const editData: model.FormEditData[] = instance.data[change.id];
            if (editData && editData.length > 0) {
              const edit = deepClone(editData[editData.length - 1]);
              edit.after.forEach((item) => {
                for (const fieldChange of change.fieldChanges) {
                  switch (fieldChange.id) {
                    case 'belongId':
                      item.belongId = this.task.taskdata.belongId;
                      break;
                    default:
                      // if (fieldChange.before) {
                      //   if (item[fieldChange.id] != fieldChange.before) {
                      //     throw new Error(
                      //       `当前字段${fieldChange.name}不为${fieldChange.beforeName}，变更失败`,
                      //     );
                      //   }
                      // }
                      if (fieldChange.name == '审核时间') {
                        let auditTime = new Date();
                        let formatAuditTime = formatDate(
                          auditTime,
                          'yyyy-MM-dd HH:mm:ss.SSS',
                        );
                        item['T' + fieldChange.id] = formatAuditTime;
                        if (instance.node.primaryForms.length > 0) {
                          let targetId = instance.node.primaryForms[0].attributes.find(
                            (a) => a.propId == fieldChange.id,
                          )?.id;
                          if (targetId) {
                            item[targetId] = formatAuditTime;
                          }
                        }
                      } else if (fieldChange.options?.defaultType === 'currentPeriod') {
                        item[fieldChange.id] = this.task.belong.financial.current;
                      } else if (fieldChange.valueType === '时间段型') {
                        const timeId = instance.fields[change.id].filter(
                          (f) => f.valueType === '时间段型' && f.code === 'timeArray',
                        )[0].id;
                        const orderNumber = instance.fields[change.id].filter(
                          (f) => f.name === '订单编号' && f.code === 'orderNumber',
                        )[0].id;
                        const timeArray = item[timeId];
                        const orderNumberInfo = item[orderNumber];
                        timeArray.forEach((time) => {
                          if (time.source === orderNumberInfo && time.isSelected) {
                            delete time.source;
                            time.isSelected = false;
                            if (time.thisSelected) {
                              delete time.thisSelected;
                            }
                          }
                        });
                        item[timeId] = timeArray;
                      } else {
                        item[fieldChange.id] = fieldChange.after ?? '';
                      }
                      break;
                  }
                }
              });
              data.set(change.id, edit);
            }
          }
        }
      }
    }
    return false;
  }
}
